/**
 * 初始化图片详情对话框
 */
var PicInfoDlg = {
    picInfoData : {}
    , validateFields: {
        title: {
            validators: {
                notEmpty: {
                    message: '标题不能为空'
                }
            }
        },
        typeCode: {
            validators: {
                notEmpty: {
                    message: '分类不能为空'
                }
            }
        },
        pic: {
            validators: {
                notEmpty: {
                    message: '图片不能为空'
                }
            }
        }

    }
};

/**
 * 清除数据
 */
PicInfoDlg.clearData = function() {
    this.picInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PicInfoDlg.set = function(key, val) {
    this.picInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PicInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PicInfoDlg.close = function() {
    parent.layer.close(window.parent.Pic.layerIndex);
}

/**
 * 收集数据
 */
PicInfoDlg.collectData = function() {
    this
    .set('id')
    .set('title')
    .set('pic')
    .set('typeCode')
}

/**
 * 验证数据是否为空
 */
PicInfoDlg.validate = function () {
    $('#picForm').data("bootstrapValidator").resetForm();
    $('#picForm').bootstrapValidator('validate');
    return $("#picForm").data('bootstrapValidator').isValid();
}
/**
 * 提交添加
 */
PicInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/pic/add", function(data){
        Feng.success("添加成功!");
        window.parent.Pic.table.refresh();
        PicInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.picInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
PicInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/pic/update", function(data){
        Feng.success("修改成功!");
        window.parent.Pic.table.refresh();
        PicInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.picInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("picForm",PicInfoDlg.validateFields);
    // 初始化图片上传
    var avatarUp = new $WebUpload({
        uploadId:"pic"
        ,uploadBarId:"progressBar"
        ,mimeTypes:'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
        ,extensions:"gif,jpg,jpeg,bmp,png"
    });
    avatarUp.init();

});
